package net.butfly.bus.client.connection;

import net.butfly.bus.client.connection.impl.HessianConnection;
import net.butfly.bus.client.connection.impl.LocalConnection;
import net.butfly.bus.client.connection.impl.HessianConnection.HessianConfigBean;
import net.butfly.bus.config.bean.NodeBean;

public class ConnectionFactory {
	public enum Type {
		hessian, local;
		public static Type parse(String type) {
			return Type.valueOf(type.toLowerCase());
		}

		public Object getPrototype() {
			switch (this) {
			case hessian:
				return new HessianConfigBean();
			}
			return null;
		}
	}

	private static ConnectionFactory ins = new ConnectionFactory();

	public static ConnectionFactory getInstance() {
		return ins;
	}

	public Connection getConnection(NodeBean nodeBean) {
		switch (Type.parse(nodeBean.getConnection().getType())) {
		case hessian:
			HessianConfigBean config = (HessianConfigBean) nodeBean.getConnection().getConfig();
			HessianConnection conn = new HessianConnection(config.getPath(),
					config.getTimeout() != null ? Long.valueOf(config.getTimeout()) : 0, null);
			return conn;
		case local:
			return new LocalConnection();
		default:
			return null;

		}
	}
}
